package com.cfjjc.gz.common.taglib;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.log4j.Logger;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
import com.cfjjc.gz.common.cache.CacheUtil;
import com.cfjjc.gz.common.util.StringUtil;

/**
 * 根据源属性取得另外一个属性值。两种查询方式：
 * 一种根据主键key查询属性值，
 * 一种根据查询条件condnames和查询条件值condvalues进行查询。<p>
 * 
 * <b>属性说明：</b>
 * <ul>
 * <li>cachetype：缓存类型,必须.</li>
 * <li>resultprop：返回结果属性,必须</li>
 * <li>key：主键查询</li>
 * <li>condname：查询条件，多个查询条件之间用分号(;)分割</li>
 * <li>condvalues：查询条件值,多个查询条件之间用分号(;)分割，必须与查询条件个数一致</li>
 * </ul>
 * 
 * <p>
 * <b>注意：</b>
 * <ul>
 * <li>多个查询条件只支持 and 查询，不支持 or 查询</li>
 * </ul>
 */
public class PropertyTag extends TagSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6752681317726663087L;

	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(PropertyTag.class);

	private String cachetype; // 缓存类型

	private String resultprop; // 要返回的属性

	private Object key; // 主键值

	private String condnames;// 查询条件名称,多个名称用分号分隔

	private String condvalues;// 查询条件值,值之间用分号分隔

	public int doStartTag() {
		Object result = "";
		try {
			if (key != null && !String.valueOf(key).equals("")) {
				result = CacheUtil.getProperty(cachetype, resultprop, key);
			
			} else {
				
				logger.debug("*****condnames values:******"+condnames);
				logger.debug("*****condvalues values:******"+condvalues);
				
				if (StringUtil.isEmpty(condnames) ||
					StringUtil.isEmpty(condvalues)) {
					throw new IllegalArgumentException("查询条件与查询值不能为空");
				}

				String[] names = condnames.split(";");
				String[] values = condvalues.split(";");
				
				if (names.length != values.length) {
					throw new IllegalArgumentException("查询条件与查询值个数不匹配");
				}
				
				Map<String, Object> queryMap = new HashMap<String, Object>();
				for (int i = 0; i < names.length; i++) {
					queryMap.put(names[i], values[i]);
				}
				
				result = CacheUtil.getProperty(cachetype, resultprop, queryMap);
			}
			if (result == null) {
				result = "";
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			return SKIP_PAGE;
		} finally {
			try {
				pageContext.getOut().print(result);
			} catch (Exception e) {
				logger.error(e.getMessage());
				return SKIP_PAGE;
			}
		}
		return EVAL_PAGE;
	}

	public Object getKey() {
		return key;
	}

	public void setKey(Object key) throws JspException {
		if (key != null) {
			this.key = ExpressionEvaluatorManager.evaluate("key", String
					.valueOf(key), Object.class, this, pageContext);
		} else {
			this.key = null;
		}
	}

	public String getResultprop() {
		return resultprop;
	}

	public void setResultprop(String resultprop) {
		this.resultprop = resultprop;
	}

	public String getCachetype() {
		return cachetype;
	}

	public void setCachetype(String cachetype) {
		this.cachetype = cachetype;
	}

	public String getCondnames() {
		return condnames;
	}

	public void setCondnames(String condnames) {
		this.condnames = condnames;
	}

	public String getCondvalues() {
		return condvalues;
	}

	public void setCondvalues(String condvalues) {
		this.condvalues = condvalues;
	}
}
